#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/7/21  9:35
# @Author: 余浪人
# @email: yulangren520@gmail.com
import re
from apps.control import sys_bp
from flask_login import login_required
from flask import redirect, current_app, jsonify, render_template, request, url_for
from apps.lib.alipay import aliPay, verify_pay_statu, save_order, save_goods
from apps.models.content_model import TL_Content_Model
from apps.lib.public import check_obj, get_uuid16, page_auto
from apps.models.order_model import TL_PayOrder_Model
@sys_bp .route ('/pay/<uid>',endpoint ='pay',methods =['GET'])
def pay (uid ):
    OO0O0O0OOO0OO00OO =TL_Content_Model .query .filter_by (status =1 ,uuid =uid ).first ()
    if not OO0O0O0OOO0OO00OO is None and OO0O0O0OOO0OO00OO .permissions :
        OOO0O000O0OO0O0O0 =aliPay ()
        O00O00000000000OO =get_uuid16 ()
        try :
            OOOOOO00OO0OO000O =OOO0O000O0OO0O0O0 .api_alipay_trade_page_pay (subject =f"购买内容: {OO0O0O0OOO0OO00OO.title}",out_trade_no =O00O00000000000OO ,total_amount =str (OO0O0O0OOO0OO00OO .reward ),return_url =f'{url_for("sys_bp.verify_pay")}')
        except AttributeError :return jsonify (OOO0O000O0OO0O0O0 )
        except TypeError :return {'status':401 ,'msg':'密钥配置错误'}
        save_goods ([{"sub_uuid":uid ,"order_id":O00O00000000000OO }])
        return redirect (current_app .config ['ALIPAY_URL']+"?"+OOOOOO00OO0OO000O )
    return jsonify ({"status":400 ,'msg':'内容获取失败,请勿非法操作'})
@sys_bp .route ('/verify_pay',endpoint ='verify_pay',methods =['GET'])
def verify_pay ():
    OOOO0OOO0000OO0O0 =verify_pay_statu ()
    if OOOO0OOO0000OO0O0 .get ('status')==0 :
        save_order (OOOO0OOO0000OO0O0 .get ('data'))
        return jsonify ({'status':0 ,'msg':'支付成功'})
    return jsonify ({'status':1 ,'msg':'支付失败'})
@sys_bp .route ('/query_order',endpoint ='query_order',methods =['POST'])
def query_order ():
    O0O0OO0O0O0O00O0O =request .form .get ('order_id','')
    O0000OO00OO000OO0 =request .form .get ('trade_no','')
    if not all ([O0O0OO0O0O0O00O0O ,O0000OO00OO000OO0 ]):return jsonify ({'status':1 ,'msg':'请输入订单编号'})
    O0O00O00000O00O0O =aliPay ()
    try :
        O00O0OO0OO00000O0 =O0O00O00000O00O0O .api_alipay_trade_query (out_trade_no =O0O0OO0O0O0O00O0O ,trade_no =O0000OO00OO000OO0 )
        if O00O0OO0OO00000O0 .get ("trade_status","")=="TRADE_SUCCESS"and O00O0OO0OO00000O0 .get ('code')=='10000':
            save_order (O00O0OO0OO00000O0 )
            return jsonify ({'status':0 ,'msg':'订单到已有订单!','data':O00O0OO0OO00000O0 })
        return jsonify ({'status':1 ,'msg':'未查询到有效订单!'})
    except AttributeError :
        return O0O00O00000O00O0O
@sys_bp .route ('/order_list',endpoint ='order_list',methods =['GET'])
@login_required
def order_list ():
    OOO00OO0000O0O0O0 =request .args .get ('search',None )
    if OOO00OO0000O0O0O0 :
        OO0000O0OO0OO00OO ,O0000O0OOO00O000O =page_auto (TL_PayOrder_Model .query .filter (TL_PayOrder_Model .out_trade_no ==OOO00OO0000O0O0O0 ))
    else :
        OO0000O0OO0OO00OO ,O0000O0OOO00O000O =page_auto (TL_PayOrder_Model .query .order_by (TL_PayOrder_Model .id .desc ()))
    return render_template ('system_template/order_list.html',data_list =OO0000O0OO0OO00OO ,page =O0000O0OOO00O000O )


